//
//  StepProvider.swift
//  Pods
//
//  Created by 李然 on 2022/9/1.
//

import UIKit
import SwiftDate
import HandyJSON
import MYModule
import RxSwift
import RxCocoa
import RxSwiftExt


open class StepProvider: ViewModel {

	private(set) var date: Date

	public init(date: Date) {
		self.date = date
		super.init()
	}

	// 最近7天数据
	open var sevenDaysRecord: [ActivityModel] {
        let list = ActivityModel.sevenday(date: date)
        
        // 查询默认设置
        let model = ExerciseGoalsModel.currentGoal(weekDay: date.weekday)
        let step_target = model.step
        let calories_target = model.kcal
        let duration_target = model.exerciseTime
        
        // 没有目标设置，采用默认目标设置
        list.forEach {
            
            if $0.goalStep == 0 {
                $0.goalStep = step_target
            }
            if $0.goalCalorie == 0 {
                $0.goalCalorie = calories_target
            }
            if $0.goalTime == 0 {
                $0.goalTime = duration_target
            }
        }
        
        
		return list
	}

	// 本周数据
	open var weekRecord: [ActivityModel] {
		var list = ActivityModel.weekDay(date: date)
        
        if list.count < 7 {
            
          let emptyData = (list.count ..< 7).map { _ in
             ActivityModel()
            }
            
            list.append(contentsOf: emptyData)
            
        }
        
		return list
	}

	// 当月数据
	open var monthRecord: [ActivityModel] {
        let list = ActivityModel.month(date: date)
        return list
	}

	// 获取当天的步数记录数据
	open var curDayStepRecord: ActivityModel {
        
        guard let model = ActivityModel.day(date: self.date) else {
            let model = ActivityModel()
            model.date = self.date
            return model
        }
        
        return model
	}

	// 获取当天详情数据
    open var curDayDetail: [Int] {
        
        let stepData = ActivityModel.day(date: date)
        let list = self.stepsDetail(of: stepData)
        return list
        

	}
    
    fileprivate func stepsDetail(of model: ActivityModel?) ->[Int] {
        
        let detail = model?.steps.map { $0 > 0 ? $0 : 1 }
        guard var detail = detail else {
            
            return (0..<48).map { _ in 0 }
        }
        if detail.count < 48 {
            
            let emptyData = (detail.count ..< 48).map { _ in 0 }
            detail.append(contentsOf: emptyData)
        }
        
        return detail
    }

	// 获取当天有效活动量
	open func getDayValidTotal() -> Int {
		
        return curDayStepRecord.time >= 600 ? curDayStepRecord.time/60 : 0
		
	}

	// 获取本周累计活动量
	open func getWeekValidTotal() -> Int {
		
            weekRecord.filter { $0.time >= 600 }.reduce(0, { $0 + $1.time })
		
	}

	// 获取本周的详情数据
	open func getWeekDetail() -> [Int] {
       return weekRecord.map { $0.step  }
	}

	// 获取本月详情数据
	open func getMonthDetail() -> [Int] {
		
			return monthRecord.map { $0.step }
		
	}

	// 和同类用户相比
	open func getCompareResult() -> (index: Int, percentage: Int) {

		let step_area: Int = 3000

		let step_area_percentage: [Int] = [23, 18, 17, 14, 11, 9, 7]
        
        let percentage_max: Int = 99
        
        let record = self.curDayStepRecord
        let step = record.step
        var result = 0
        if step <= 0 {
            return (0, 0)
        }
        
        let index = step / step_area
        let remainder = step % step_area
        
        if index >= step_area_percentage.count {
            return (step_area_percentage.count, percentage_max)
        }
        
        for i in 0..<index {
            result += step_area_percentage[i]
        }
        
        let percentage = Double(remainder) / Double(step_area) * Double(step_area_percentage[index])
        
        return (index, result + Int(percentage))
    }
    
}
